<template>
  <el-dialog :visible.sync="dialogVisible" width="800px" :before-close="handleClose" :close-on-click-modal="false"
    append-to-body title="药品配送" center>
    <el-form :model="form" ref="formRef" label-width="100px"
      style="max-height: 580px; overflow: hidden;overflow-y: scroll;">

    <!--   <el-form-item prop="type" label="类型" :rules="[{ required: true, message: '请选择类型', trigger: 'blur' }]">
        <el-select @change="$forceUpdate()" v-model="form.type" clearable filterable placeholder="请选择">
          <el-option v-for="item in typeArr" :key="item.name" :label="item.name" :value="item.code">
          </el-option>
        </el-select>
      </el-form-item> -->

     
      <!-- 
      <el-form-item prop="sub_department_id" label="科室"
        :rules="[{ required: true, message: '请选择机构名称', trigger: 'blur' }]">
        <el-select v-model="form.sub_department_id" clearable filterable placeholder="请选择">
          <el-option v-for="item in subDepartMentArr" :key="item.name" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item> -->

      <el-form-item label="快递单号" prop="name" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.name"></el-input>
      </el-form-item>
      

     <!--  <el-form-item label="药品单价" prop="price" :rules="[{ required: true, message: '请输入药品单价', trigger: 'blur' }]">
        <el-input type="number" placeholder="请输入" v-model="form.price"></el-input>
      </el-form-item>

      <el-form-item label="药品介绍" prop="introduce" :rules="[
          { required: true, message: '请输入药品介绍', trigger: 'blur' },
        ]">
        <el-input resize="none" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入"
          v-model="form.introduce"></el-input>
      </el-form-item>

      <el-form-item label="药品规格" prop="size" :rules="[{ required: true, message: '请输入图文问诊价格', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.size"></el-input>
      </el-form-item>

      <el-form-item label="药品成分" prop="ingredient" :rules="[{ required: true, message: '请输入药品成分', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.ingredient"></el-input>
      </el-form-item>

      <el-form-item label="功能主治/适应症" prop="feature" :rules="[{ required: true, message: '请输入', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.feature"></el-input>
      </el-form-item>

      <el-form-item label="存储条件" prop="save" :rules="[{ required: true, message: '请输入存储条件', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.save"></el-input>
      </el-form-item>

      <el-form-item label="注意事项" prop="note" :rules="[{ required: true, message: '请输入注意事项', trigger: 'blur' }]">
        <el-input placeholder="请输入" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }"
          v-model="form.note"></el-input>
      </el-form-item>

      <el-form-item label="用药指导" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" prop="guide"
        :rules="[{ required: true, message: '请输入用药指导', trigger: 'blur' }]">
        <el-input placeholder="请输入" v-model="form.guide"></el-input>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="tx-c">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="confirm" :loading="btnLoading">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import { debounce } from "@/util";
import { ApiMedicineEdit, ApiMedicineAdd } from "@/api/index.js";
import ImgUploadPath from "@/components/imgUpload-path.vue";
import { validatePhone } from "@/util/validate";
export default {
  props: ["roleData", "departmentData", "unitList", "subDepartMentArr"],
  components: {
    ImgUploadPath,
  },
  data() {
    return {
      validatePhone,
      //职称
      //医生职称
      docLevel: [
        { name: "副主任医师", code: "副主任医师" },
        { name: "主任医师", code: "主任医师" },
        { name: "主治医师", code: "主治医师" },
        { name: "医师", code: "医师" },
        { name: "医士", code: "医士" },
      ],
      //护士职称
      huLevel: [
        { name: "副主任护师", code: "副主任护师" },
        { name: "主任护师", code: "主任护师" },
        { name: "主管护师", code: "主管护师" },
        { name: "护师", code: "护师" },
        { name: "护士", code: "护士" },],
      yaoLevel: [
        { name: "‌副主任药师", code: "‌副主任药师" },
        { name: "主任药师", code: "主任药师" },
        { name: "‌主管药师", code: "‌主管药师" },
        { name: "‌药师", code: "‌药师" },
        { name: "‌药士", code: "‌药士" },],

      optionsData: [
        { name: "男", code: "男" },
        { name: "女", code: "女" },
      ],
      levelArr: [
        { name: "普通", code: "普通" },
        { name: "中级", code: "中级" },
        { name: "高级", code: "高级" },
      ],
      typeArr: [
        { name: "处方药", code: "处方药" },
        { name: "非处方药", code: "非处方药" },
      ],
      commentArr: [
        { name: "慢病药物方向", code: "慢病药物方向" },
        { name: "精神药物方向", code: "精神药物方向" },
        { name: "肿瘤药物方向", code: "肿瘤药物方向" },
      ],
      //接诊方式
      /* consultation_type */
   /*    consultation_typeArr: [
        { name: "全部", code: "0" },
        { name: "图文问诊", code: "1" },
        { name: "视频问诊", code: "2" },
      ], */

      oldform: {
        name: "",
        common_name: "",
        type: "",
        icon: "",
        introduce: "",
        price: "",
        size: "",
        ingredient: "",
        feature: "",
        save: '',
        note: '',
        guide: '',
      },
      form: {},
      btnLoading: false,
      dialogVisible: false,
      id: null,
      title: "",
      imgsDefalut: [],
      roleArr: [{ name: '医师', code: '医师' }, { name: '护师', code: '护师' }]
    };
  },
  computed: {
    ...mapState(["userState"]),
  },
  methods: {
    /*   getUnitList() {
        ApigetUnitList({ page: 1, size: 999 }).then(res => {
          this.unitListArr = res.data
        })
      }, */
    onUploadSuccess(path) {
      this.form.pic = path;
    },
    onUploadRemove() {
      this.form.pic = "";
    },
    confirm: debounce(
      function () {
        this.$refs["formRef"].validate(async (valid) => {
          if (valid) {
            this.btnLoading = true;

            try {
              if (this.id) {
                await ApiMedicineEdit(this.form, this.id);
                this.$message.success("修改成功");
              } else {
                await ApiMedicineAdd(this.form);
                this.$message.success("添加成功");
                /* this.$alert("添加成功，该员工初始密码为：123456", "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {},
                }); */
              }

              this.handleClose();
              this.$emit("refresh");
            } finally {
              this.btnLoading = false;
            }
          } else return false;
        });
      },
      500,
      true
    ),

    show(row) {
    /*   console.log(this.unitList)
      const data = row || this.oldform;
      for (const key in this.oldform) {
        this.$set(this.form, key, data[key]);
      }
      if (row) {
        this.id = row.id;
        this.title = "编辑药品";
        if (row.pic) this.imgsDefalut = [{ url: this.$path + row.pic }];
      } else {
        this.id = null;
        this.title = "添加药品";
      } */
      this.dialogVisible = true;
    },

    handleClose() {
      this.dialogVisible = false;
      this.form = { ...this.oldform };
      this.$nextTick((_) => {
        this.$refs.formRef.clearValidate();
        this.$refs.ImgUploadPath.clear();
      });
    },
  },
};
</script>

<style></style>
